/*
Uninclude: Tool for detecting unused #included files in C and C++ code.
    Copyright (C) 2012  Daniel Gutson, FuDePAN

    Uninclude is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version./

    Uninclude is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Uninclude.  If not, see <http://www.gnu.org/licenses/>.

    This is an utility to manipulate full paths and dirs.
    Replaces awful C's basedir() and dirname()
*/

#ifndef DIR_UTILS_H
#define DIR_UTILS_H

#include <string>
using namespace std;

inline string removeQuotes(const string& name)
{
    string ret;
    const size_t nameSize = name.size();

    if (nameSize > 0)
    {
        
        if (name[0] == '\"')
        {
            if ( nameSize >= 2 && *name.rbegin() == '\"' )
                ret = name.substr(1, nameSize - 2);
            else
                throw "Malformed name with quotes.";
        }
        else
            ret = name;
    }

    return ret;
}

inline string getBaseDir(const string& fullPath, bool includeSlashInDir = true)
{
    const string::size_type last_slash = fullPath.find_last_of('/');
    if ( last_slash == string::npos )
        return string();
    else
        return fullPath.substr(0, last_slash + (includeSlashInDir ? 1 : 0));
}

inline void splitDirFile(const string& fullPath, string& baseDir, string& file, bool includeSlashInDir = true)
{
    baseDir.clear();
    const string::size_type last_slash = fullPath.find_last_of('/');
    if ( last_slash == string::npos )
        file = fullPath;
    else
    {
        baseDir = fullPath.substr(0, last_slash + (includeSlashInDir ? 1 : 0));
        file = fullPath.substr(last_slash + 1);
    }
}

inline string makeRelative(const string& fullPath, const string& baseDir)
{
    const bool makeRelative = (!baseDir.empty() && fullPath.find(baseDir) == 0);

    if (makeRelative)
        return fullPath.substr(baseDir.size());
    else
        return fullPath;
}

#endif

